package com.fiberhome.nas.core.util.media

import android.net.Uri
import com.fiberhome.nas.core.model.entity.AlbumInfo
import com.fiberhome.nas.core.model.entity.LocalMediaItem
import com.fiberhome.nas.core.model.entity.MediaType
import com.fiberhome.nas.core.model.entity.TaskStatus
import com.fiberhome.nas.core.model.entity.UploadTask
import com.fiberhome.nas.core.util.file.FileUtils
import java.io.File
import java.util.Date
import java.util.UUID

/**
 * 媒体转换工具类
 */
object MediaConverter {

    /**
     * 将LocalMediaItem转换为UploadTask
     *
     * @param mediaItem 本地媒体项对象
     * @param album 相册信息
     * @param chunkSize 切片大小
     * @return 上传任务对象
     */
    fun localMediaItemToUploadTask(
        mediaItem: LocalMediaItem,
        album: AlbumInfo,
        chunkSize: Int
    ): UploadTask {
        val file = File(mediaItem.path)
        return UploadTask().apply {
            id = UUID.randomUUID().toString()
            fileName = file.name
            fileSize = file.length()
            filePath = file.absolutePath
            fileType = FileUtils.getFileExtension(file.name) //todo
            status = TaskStatus.Waiting
            uploadPath = album.albumName
            uploadedSize = 0
            totalSize = file.length()
            currentChunk = 0
            totalChunk = ((file.length() + chunkSize - 1) / chunkSize).toInt()
            labelTip = "等待上传"
            favorite = false //上传无收藏属性
            duration = mediaItem.duration
            albumId = album.id
            mediaId = mediaItem.id.toString()
        }
    }


    /**
     * 将UploadTask转换为LocalMediaItem
     *
     * @param task 上传任务
     * @return 本地媒体项对象
     */
    fun uploadTaskToLocalMediaItem(task: UploadTask): LocalMediaItem {
        val file = File(task.filePath)
        val fileUri = Uri.fromFile(file)

        // 根据文件扩展名判断媒体类型
        val mediaType = when {
            isImageFile(task.fileType) -> MediaType.PHOTO
            isVideoFile(task.fileType) -> MediaType.VIDEO
            isAudioFile(task.fileType) -> MediaType.AUDIO
            else -> MediaType.OTHER
        }

        return LocalMediaItem(
            id = task.mediaId.toLongOrNull() ?: 0L,
            uri = fileUri,
            type = mediaType,
            displayName = task.fileName,
            mimeType = getMimeTypeFromExtension(task.fileType),
            dateTaken = Date(), // 使用当前日期，因为原始日期可能不可用
            duration = task.duration,
            fileSize = task.fileSize,
            width = 0, // 这些信息在UploadTask中不可用，设置为默认值
            height = 0,
            path = task.filePath
        )
    }

    /**
     * 根据文件扩展名判断是否为图片文件
     */
    private fun isImageFile(extension: String): Boolean {
        val imageExtensions = listOf("jpg", "jpeg", "png", "gif", "bmp", "webp")
        return imageExtensions.contains(extension.lowercase())
    }

    /**
     * 根据文件扩展名判断是否为视频文件
     */
    private fun isVideoFile(extension: String): Boolean {
        val videoExtensions = listOf("mp4", "avi", "mkv", "mov", "wmv", "flv", "webm")
        return videoExtensions.contains(extension.lowercase())
    }

    /**
     * 根据文件扩展名判断是否为音频文件
     */
    private fun isAudioFile(extension: String): Boolean {
        val audioExtensions = listOf("mp3", "wav", "ogg", "aac", "flac", "m4a")
        return audioExtensions.contains(extension.lowercase())
    }

    /**
     * 根据文件扩展名获取MIME类型
     */
    private fun getMimeTypeFromExtension(extension: String): String {
        return when {
            isImageFile(extension) -> "image/${extension.lowercase()}"
            isVideoFile(extension) -> "video/${extension.lowercase()}"
            isAudioFile(extension) -> "audio/${extension.lowercase()}"
            else -> "application/octet-stream"
        }
    }
}